$( document ).ready(function() {
	var obj = $("#dragandrophandler");
	
	obj.on('dragenter', function (e)
	{
		e.stopPropagation();
		e.preventDefault();
		$(this).css('border', '2px solid #0B85A1');
	});
	
	obj.on('dragover', function (e)
	{
		 e.stopPropagation();
		 e.preventDefault();
	});
	
	obj.on('drop', function (e)
	{
		 $(this).css('border', '2px dotted #0B85A1');
		 e.preventDefault();
		 var files = e.originalEvent.dataTransfer.files;
	 
		 //We need to send dropped files to Server
		 handleFileUpload(files,obj);
	});
	
	$(document).on('dragenter', function (e)
	{
		e.stopPropagation();
		e.preventDefault();
	});
	
	$(document).on('dragover', function (e)
	{
	  e.stopPropagation();
	  e.preventDefault();
	  obj.css('border', '2px dotted #0B85A1');
	});
	
	$(document).on('drop', function (e)
	{
		e.stopPropagation();
		e.preventDefault();
	});
	
	function handleFileUpload(files,obj)
	{
		try
		{
			angular.element($('#main_container')).scope().setAvatarCommand(files[0]);
		}
		catch(err)
		{
			alert(err);
		}
	}
});

app.controller('datacontroller', ['$scope', '$parse', '$http', function($scope, $parse, $http)
{
	$scope.isComponentLoading = true;
	$scope.currentUser = undefined;
	$scope.servicebase = '../../Core/server.class.php';
	
	// listen for the event in the relevant $scope
	$scope.$on('currentUser', function (event, data) {
		$scope.currentUser = data.user;
	});
	
	/**
	*	Get all articles
	*/
	$scope.getArticlesCommand = function()
	{
		console.info("getArticlesCommand - called"); 
		$scope.loadingArticles = true;
		$http.post($scope.servicebase, { "service" : "getArticles"}).
		success(function(data, status) 
		{
			console.info("getArticlesCommand - call success"); 
			$scope.status = status;
			$scope.data = data;
			$scope.result = data; 
			$scope.loadingArticles = false;
			
			if(data.isFailed == true)
			{
				$scope.setMessageCommand($scope.data.exception, 'danger');
			}
			
			$scope.articles = $scope.data.response;
		})
		.
		error(function(data, status) 
		{
			console.error("getArticlesCommand - call failed"); 	
			$scope.loadingArticles = false;
			$scope.data = data || "Request failed";
			$scope.status = status;			
			console.error("getArticlesCommand - data : " + $scope.data); 				
		});
	};
	
	/**
	* Format date method
	*/
	$scope.parseDate = function(date)
	{
		var dateStringArray = date.split(" ");
		var firstPartDate = dateStringArray[0];
		var firstPartDateArray = firstPartDate.split("-");
		var secondPartDate = dateStringArray[1];
		var month = parseInt(firstPartDateArray[1]) - 1;
		var parsedDate = undefined;
		if(secondPartDate != undefined)
		{
			var secondPartDateArray = secondPartDate.split(":");
			parsedDate = new Date(firstPartDateArray[0], month, firstPartDateArray[2], secondPartDateArray[2], secondPartDateArray[1], secondPartDateArray[0], 0);
		}
		else
		{
			parsedDate = new Date(firstPartDateArray[0], month, firstPartDateArray[2]);
		}
		var formattedMonth = parseInt(parsedDate.getMonth()) + 1;
		return parsedDate.getDate() + "/" + formattedMonth + "/" + parsedDate.getFullYear(); 
	};
	
	/**
	* Check all user's state
	*/
	$scope.checkUsersStatesCommand = function()
	{
	}
	
	/**
	*	Load the admin component
	*/
	$scope.loadAdminComponentCommand = function() 
	{
		console.info("loadAdminComponentCommand - called");
		
		console.info("Set variables");
		$scope.articles = [];
		$scope.events = [];
		$scope.tchatMessages = [];
		$scope.translations = [];
		$scope.eventStates = [];
		$scope.archives = [];
		$scope.materials = []; 
		$scope.materialsState = [];
		$scope.users = [];
		$scope.profiles = [];
		$scope.Avatar = undefined;
		$scope.lastTchatMessageId = 0;
		$scope.Poster = undefined;
		$scope.isComponentLoading = false;
		
	};
	
	$scope.loadAdminComponentCommand();
	
	/*
	* Check all users states each n seconds
	*/
	setInterval(function()
	{
		$scope.checkUsersStatesCommand();
	}, 5000);
}]);